using System;
using System.Reflection;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;

using System.Security;
using System.Security.Permissions;
using System.Web;
using System.Web.Security;
using Microsoft.SharePoint.Security;

using stsdev;

namespace WFActivitySolutionProvider{
  public class ActivitySP : ISolutionProvider {

    public string Title {
      get { return "Custom Workflow Activity Generator"; }
    }

    //Added 2/10/08 DMANN
    public bool LaunchSolutionOnComplete
    {
        get { return true; }
    }



public void InitializeSolution() {
      SolutionBuilder.AssemblyDeployment = true;
      SolutionBuilder.SafeControlSettings = false;
      SolutionBuilder.DeploymentTarget = AssemblyDeploymentTarget.GlobalAssemblyCache;
      SolutionBuilder.CasPermissions = false;
    }

    public void AddSolutionItems() {
        //Add Assembly References to Project File
        SolutionBuilder.ProjectAssemblyReferences.Add("System.Drawing");
        SolutionBuilder.ProjectAssemblyReferences.Add("Microsoft.SharePoint.WorkflowActions");
        SolutionBuilder.ProjectAssemblyReferences.Add("System.Workflow.Activities");
        SolutionBuilder.ProjectAssemblyReferences.Add("System.Workflow.ComponentModel");
        SolutionBuilder.ProjectAssemblyReferences.Add("System.Workflow.Runtime");
        SolutionBuilder.ProjectAssemblyReferences.Add("System.Design");
        SolutionBuilder.ProjectAssemblyReferences.Add("System.Drawing.Design");
        SolutionBuilder.ProjectAssemblyReferences.Add("System.Windows.Forms");
        

        string sActivityName = (string)InputForm.InputBox("Enter name for Activity (cannot be the same as Solution Name)", "STSDev", "Custom_Activity");
        if (string.IsNullOrEmpty(sActivityName))
        {
            sActivityName = "Custom_Activity";
        }
        if (sActivityName.ToLower() == SolutionBuilder.SolutionName.ToLower())
        {
            sActivityName = "Custom_Activity";
        }
        ActivityClassesBuilder.CreateActivityClasses(sActivityName);
        ActivityActionsFileBuilder.CreateActionsFile(sActivityName);

        //Add ActivityLogo image as a Resource
        if (!Directory.Exists("Resources"))
        {
            Directory.CreateDirectory("Resources");
        }
        Properties.Resources.ActivityLogo.Save(@"Resources\ActivityLogo.gif");
        ResourceBuilder.CreateFileResource("ActivityLogo", Image.FromFile(@"Resources\ActivityLogo.gif"));
        ProjectFileBuilder.AddSourceFile(@"Resources\ActivityLogo.gif", SourceFileTypeEnum.Content);

        //Add Feature with Receiver and custom seed code
        Globals.FeatureActivatedCode.Append("SPWebApplication wappCurrent = (SPWebApplication)properties.Feature.Parent;");
        Globals.FeatureActivatedCode.Append("\r\n\t\t");
        Globals.FeatureActivatedCode.Append("SPWebConfigModification modAuthorizedType = new SPWebConfigModification();");
        Globals.FeatureActivatedCode.Append("\r\n\t\t");
        Globals.FeatureActivatedCode.Append("modAuthorizedType.Name = \"AuthType\";");
        Globals.FeatureActivatedCode.Append("\r\n\t\t");
        Globals.FeatureActivatedCode.Append("modAuthorizedType.Owner = \"" + SolutionBuilder.SolutionName + "." + sActivityName + "\";");
        Globals.FeatureActivatedCode.Append("\r\n\t\t");
        Globals.FeatureActivatedCode.Append("modAuthorizedType.Path = \"configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes\";");
        Globals.FeatureActivatedCode.Append("\r\n\t\t");
        Globals.FeatureActivatedCode.Append("modAuthorizedType.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;");
        Globals.FeatureActivatedCode.Append("\r\n\t\t");
        Globals.FeatureActivatedCode.Append("modAuthorizedType.Value = @\"<authorizedType Assembly='" + SolutionBuilder.FullAssemblyName + "' Namespace='" + SolutionBuilder.SolutionName + "' TypeName='*' Authorized='True' />\";");
        Globals.FeatureActivatedCode.Append("\r\n\t\t");
        Globals.FeatureActivatedCode.Append("wappCurrent.WebConfigModifications.Add(modAuthorizedType);");
        Globals.FeatureActivatedCode.Append("\r\n\t\t");
        Globals.FeatureActivatedCode.Append("wappCurrent.WebService.ApplyWebConfigModifications();");
        Globals.FeatureActivatedCode.Append("\r\n\t\t");
        //save Feature Images to proper directory
        if (!Directory.Exists(Globals.ImagesFolder + @"\" + SolutionBuilder.SolutionName))
        {
            Directory.CreateDirectory(Globals.ImagesFolder + @"\" + SolutionBuilder.SolutionName);
        }
        Properties.Resources.ActivityLogo.Save(@"RootFiles\Template\Images\" + SolutionBuilder.SolutionName + @"\ActivityLogo.gif");
        Properties.Resources.ActivityIcon.Save(@"RootFiles\Template\Images\" + SolutionBuilder.SolutionName + @"\ActivityIcon.gif");
        ProjectFileBuilder.AddSourceFile(@"RootFiles\Template\Images\" + SolutionBuilder.SolutionName + @"\ActivityLogo.gif", SourceFileTypeEnum.Content);
        ProjectFileBuilder.AddSourceFile(@"RootFiles\Template\Images\" + SolutionBuilder.SolutionName + @"\ActivityIcon.gif", SourceFileTypeEnum.Content);

        //create Feature
        FeatureBuilder.Create(SolutionBuilder.SolutionName, FeatureScopeEnum.WebApplication, true, Globals.ImagesFolder + @"\" + SolutionBuilder.SolutionName + @"\ActivityIcon.gif");
    }  
  }
}
